let Sequelize = require('sequelize');
let Op = Sequelize.Op;

let Department = require('../models/Department');
let attributes = [
    'id',
    'name'
];

class DepartmentService {
    static async getPartList() {
        try {
            return await Department.findAll({ attributes });
        } catch (e) {
            return e.stack;
        }
    }
    static async findPartByName({ name }) {
        try {
            return await Department.findOne({attributes, where: { name }})
        } catch (e) {
            return e.stack;
        }
    }
    static async createPart(param) {
        let part = await this.findPartByName(param);
        if(typeof part === 'string') return part;
        if(part) {
            return 'Error: 部门名称已存在，请重新输入';
        }else{
            try {
                return await Department.create(param)
            } catch (e) {
                return e.stack
            }
        }
    }
}

module.exports = DepartmentService;